package com.jtpay.sdk.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Joe on 2017/3/29 0029 15:06.
 * SharedPreferencesManager local data storage
 */

public class SharedPreferencesManager {

    /*constants*/
    public static final String PATTERN_FILE_NAME = "pattern_msg";
    public static final String USER_ID = "user_id";

    /*consants*/
    private static SharedPreferences mSP;
    private static SharedPreferences.Editor mEditor;
    private static SharedPreferencesManager instance;
    private Context mContext;

    private SharedPreferencesManager(Context context) {
        mContext = context;
        if (null == mSP) {
            mSP = mContext.getSharedPreferences(PATTERN_FILE_NAME, Context.MODE_PRIVATE);
        }
        if (null == mEditor) {
            mEditor = mSP.edit();
        }
    }

    public synchronized static SharedPreferencesManager getInstance(Context context) {
        if (null == instance) {
            synchronized (SharedPreferencesManager.class) {
                if (null == instance) {
                    instance = new SharedPreferencesManager(context);
                }
            }
        }
        return instance;
    }

    public String readString(String key, String defValue) {
        return mSP.getString(key, defValue);
    }

    public void saveString(String key, String value) {
        mEditor.putString(key, value);
        mEditor.apply();
    }

    public int readInt(String key, int defValue) {
        return mSP.getInt(key, defValue);
    }

    public void saveInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.apply();
    }

    public long readLong(String key, long defValue) {
        return mSP.getLong(key, defValue);
    }

    public void saveLong(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.apply();
    }


    public boolean readBoolean(String key, boolean defValue) {
        return mSP.getBoolean(key, defValue);
    }

    public void saveBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.apply();
    }

    public void clean() {
        mEditor.clear();
        mEditor.apply();
    }

}
